<template>
	<view class="page">
		<u-navbar title="员工列表" border @leftClick="$utils.back()" placeholder>
		</u-navbar>
		<filter @reload='reload' />
		<view style="flex: 1;margin-top: 20rpx;">
			<z-paging ref="paging" style="height: 100%;" :fixed="false" v-model="data" @query="query"
				:default-page-no="1" :default-page-size="10">
				<template v-if="!loading">
					<view class="userInfoBox" v-for="item in data" :key="item.id">
						<u--image :fade='false' :src="item.user.avatar" width="100rpx" height="100rpx"
							radius='8rpx'></u--image>
						<view>
							<view class="flex">
								<u-text :lines='1' size='24rpx' :text='item.user.nick_name'></u-text>
								<view @click='openSelect(item)'>
									<u-icon name="more-dot-fill" size="30rpx"></u-icon>
								</view>
							</view>
							<view style="display: flex;flex-wrap: wrap;">
								<view v-for="(item,index) in item.roles" :key="index" style="margin:10rpx 10rpx 0 0;" >
									<u-tag :text="item.description" plain size="mini" ></u-tag>
								</view>
							</view>
						</view>
					</view>
				</template>
				<template v-else>
					<view class="userInfoBox" v-for="item in 20" :key="item">
						<view style="height: 100rpx;width: 100rpx;border-radius: 8rpx;" class="Skeleton" />
						<view>
							<view style="height: 30rpx;" class="Skeleton" />
							<view style="height: 30rpx;" class="Skeleton" />
						</view>
					</view>
				</template>
			</z-paging>
		</view>
		<view class="add" @click="jumpAdd">
			<u-icon color='#fff' name='man-add' size='50rpx' />
		</view>
		<root-portal>
			<u-action-sheet :actions="columns" :show="selectShow" @close='selectShow=false' @select="confirm"
				closeOnClickOverlay></u-action-sheet>
			<u-modal :show="delShow" content='是否确认删除？' showCancelButton @confirm='del'
				@cancel='delShow=false'></u-modal>
		</root-portal>
	</view>
</template>

<script>
	import filter from '@/components_company/filter.vue'
	export default {
		components: {
			filter
		},
		data() {
			return {
				delShow: false,
				data: [],
				loading: true,
				selectItem: null,
				selectShow: false,
				columns: [{
						name: '编辑'
					},
					{
						name: '删除'
					}
				],
				keywords: ''
			}
		},
		methods: {
			reload(e) {
				this.keywords = e.keywords
				this.$refs.paging.reload()
			},
			query(page, size) {
				let obj = {
					keywords: this.keywords,
					page,
					size
				}
				uni.showLoading({
					title: '加载中...'
				})
				this.$api_company.Administrator.List(obj).then(res => {
					this.$refs.paging.complete(res.data.data.map(item => {
						return {
							...item,
							avatar: item.members?.avatar,
							nick_name: item.members?.nick_name,
							mobile: item.members?.mobile
						}
					}))
					this.loading = false
					uni.hideLoading()
				}).catch(err => {
					console.log(err)
					uni.hideLoading()
				})
			},
			del() {
				this.delShow = false
				uni.showLoading({
					title: '删除中...'
				})
				this.$api_company.Administrator.Del(this.selectItem.user_id).then(err => {
					this.data = this.data.filter(item => item.user_id != this.selectItem.user_id)
					uni.hideLoading()
				}).catch(err => {
					uni.hideLoading()
					uni.$u.toast(err.message)
				})
			},
			openSelect(item) {
				this.selectItem = item
				this.selectShow = true
			},
			confirm(e) {
				const item = e.name
				this.selectShow = false
				if (item == '编辑') {
					this.update()
				} else if (item == '删除') {
					this.delShow = true
				}
			},
			jumpAdd() {
				this.$utils.navigateTo({
					url: '/administrator/add'
				})
			},
			update() {
				this.$utils.navigateTo({
					url: '/administrator/update?id=' + this.selectItem.user_id
				})
			}
		}
	}
</script>

<style lang="scss">
	.page {
		padding: 0rpx 20rpx;
		height: 100vh;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 10rpx);
		/* 兼容 iOS 设备 */
		padding-bottom: calc(env(safe-area-inset-bottom) + 10rpx);
		box-sizing: border-box;
		display: flex;
		flex-direction: column;

		.userInfoBox {
			display: flex;
			margin-bottom: 20rpx;

			>view:nth-child(2) {
				margin-left: 10px;
				flex: 1;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				border-bottom: 1rpx solid #f1f1f1;
				padding: 0 0 10rpx 0;
			}
		}

	}

	.flex {
		display: flex;
	}



	.add {
		position: fixed;
		right: 5vw;
		bottom: 20vh;
		border-radius: 100%;
		box-shadow: 0 0 10rpx #ddd;
		background: rgba(0, 0, 0, 0.7);
		padding: 20rpx;
		z-index: 111;
	}
</style>